﻿#region Copyright (c) 2007-2010 SmartVault Corporation, all rights reserved.
// 
// http://www.opensource.org/licenses/mit-license.php
// 
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
// 
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
// 
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
// 
#endregion
using System;
using System.Windows.Forms;
using SVault.Client.SDK;

namespace SampleClient
{
    public partial class Form1 : Form
    {
        private IToolbarContext _context;
        private IToolbarNewItem _newItem;

        public Form1()
        {
            InitializeComponent();
        }

        protected override void OnShown(EventArgs e)
        {
            _context = Toolbar.Open("QuickBooks", "Example", "SmartVault Example Application");

            if (_context.ToolbarState.AssociationState == ContextAssociationState.NotProvisioned)
                _context.Provision();

            base.OnShown(e);
            tabControl1_Selected(tabControl1.SelectedTab,
                                 new TabControlEventArgs(tabControl1.SelectedTab, tabControl1.SelectedIndex,
                                                         TabControlAction.Selected));
        }

        private void tabControl1_Selected(object sender, TabControlEventArgs e)
        {
            IToolbarMessage msg;
            switch(e.TabPage.Name)
            {
                case "tabHome":
                    msg = _context.CreateMessage();
                    msg.SetObjectType("Home");
                    msg.Commit();
                    break;
                case "tabItem":
                    lbItems_SelectedIndexChanged(this, EventArgs.Empty);
                    break;
                case "tabNew":
                    msg = _context.CreateMessage();
                    _newItem = msg.SetNewItem("Customer");
                    msg.Commit();
                    btnDiscard.Enabled = btnCreate.Enabled = txtFirstName.Text.Length > 0 && txtLastName.Text.Length > 0;
                    break;
                case "tabNone":
                    _context.ClearCurrentItem();
                    break;
            }
        }

        private void lbItems_SelectedIndexChanged(object sender, EventArgs e)
        {
            IToolbarMessage msg;
            if (lbItems.SelectedIndex < 0)
            {
                _context.ClearCurrentItem();
                btnDelete.Enabled = false;
            }
            else
            {
                btnDelete.Enabled = true;
                msg = _context.CreateMessage();
                msg.SetObjectTypeWithIdentity("Customer", lbItems.Items[lbItems.SelectedIndex].ToString());
                msg.Commit();
            }
        }

        private void btnDelete_Click(object sender, EventArgs e)
        {
            if(lbItems.SelectedIndex >= 0)
            {
                lbItems.Items.RemoveAt(lbItems.SelectedIndex);
                lbItems.SelectedIndex = -1;
            }
        }

        private void NameTextChanged(object sender, EventArgs e)
        {
            btnDiscard.Enabled = btnCreate.Enabled = txtFirstName.Text.Length > 0 && txtLastName.Text.Length > 0;
        }

        private void NameTextChanged(object sender, KeyEventArgs e)
        { NameTextChanged(sender, EventArgs.Empty); }

        private void btnCreate_Click(object sender, EventArgs e)
        {
            string name = String.Format("{0} {1}", txtFirstName.Text.Trim(), txtLastName.Text.Trim());
            lbItems.Items.Add(name);
            _newItem.Commit(name);

            lbItems.SelectedItem = name;
            tabControl1.SelectedTab = tabItem;
        }

        private void btnDiscard_Click(object sender, EventArgs e)
        {
            _newItem.AbandonItem();
            txtFirstName.Text = txtLastName.Text = String.Empty;
            tabControl1.SelectedTab = tabItem;
        }
    }
}
